import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
from time import sleep

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
}


def main():
    url = 'https://music.163.com/discover/playlist/'
    data = []
    for i in range(0, 19):
        params = {
            'order': 'hot',
            'cat': '全部',
            'limit': 35,
            'offset': f'{i * 35}'
        }
        html = req(url, params)
        print(f'正在爬取第{i + 1}页...')
        playlist_urls = parse_playlist_url(html)
        sleep(2)
        for playlist_url in playlist_urls:
            playlist_html = req(playlist_url)
            data += [parse_playlist_data(playlist_html)]
            sleep(2)
    save_to_csv(data)


def req(url, params=None):
    if params is None:
        params = {}
    response = requests.get(url, headers=headers, params=params)
    return response.text


def parse_playlist_url(html):
    soup = BeautifulSoup(html, 'lxml')
    music_url = soup.find_all('a', attrs={'class': 'msk', 'href': re.compile(r'/playlist\?id=\d+')})
    return ['https://music.163.com' + i['href'] for i in music_url]


def parse_playlist_data(html):
    soup = BeautifulSoup(html, 'lxml')
    tag = '|'.join([i.text for i in soup.find_all('a', attrs={'class': 'u-tag'})])
    introduce = soup.find('p', attrs={'class': 'intr f-brk'}).text
    fav = soup.find('a', attrs={'class': re.compile(r'u-btni u-btni-fav')}).text
    share = soup.find('a', attrs={'class': re.compile(r'u-btni u-btni-share')}).text
    comment = soup.find('a', attrs={'class': re.compile(r'u-btni u-btni-cmmt')}).text
    date = soup.find('span', attrs={'class': 'time s-fc4'}).text
    title = soup.find('h2', attrs={'class': 'f-ff2 f-brk'}).text
    song_count = soup.find('span', attrs={'id': 'playlist-track-count'}).text
    return [title, date, song_count, tag, introduce, fav, share, comment]


def save_to_csv(data):
    df = pd.DataFrame(data, columns=['歌单名称', '创建时间', '歌曲数量', '标签', '简介', '收藏', '分享', '评论'])
    df.to_csv('../static/data/info.csv', index=False, encoding='utf-8-sig')


if __name__ == '__main__':
    main()